package com.scujj.cs6.controller;

import com.scujj.cs6.annotation.AuthCheck;
import com.scujj.cs6.common.Result;
import com.scujj.cs6.model.dto.answer.AnswerDTO;
import com.scujj.cs6.model.vo.AnswerVo;
import com.scujj.cs6.model.vo.MyAnswerVo;
import com.scujj.cs6.model.vo.PageVo;
import com.scujj.cs6.service.AnswerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "回答接口", description = "回答相关API")
@RestController
@RequestMapping("/answers")
@Validated
public class AnswerController {
    @Autowired
    private AnswerService answerService;

    @Operation(summary = "获取回答列表", description = "根据问题ID获取回答列表")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = AnswerVo.class))
            )
    )
    @GetMapping("/")
    public Result<PageVo<AnswerVo>> getAnswers(@Parameter(name = "page", description = "页码")
                                             @RequestParam(defaultValue = "1", required = false) int page,
                                             @Parameter(name = "size", description = "每页数量")
                                             @RequestParam(defaultValue = "10", required = false) int size,
                                             @Parameter(name = "questionId", description = "问题ID")
                                             @RequestParam Long questionId) {
        return Result.success(answerService.getAnswers(page, size, questionId));
    }

    @Operation(summary = "获取我的回答", description = "获取我的回答及对应的问题列表")
    @GetMapping("/my")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = AnswerVo.class))
            )
    )
    public Result<PageVo<MyAnswerVo>> getMyAnswers(@Parameter(name = "page", description = "页码")
                                              @RequestParam(defaultValue = "1", required = false) int page,
                                                   @Parameter(name = "size", description = "每页数量")
                                              @RequestParam(defaultValue = "10", required = false) int size,
                                                   HttpServletRequest request) {
        return Result.success(answerService.getMyAnswers(page, size, request));
    }

    @Operation(summary = "获取我关注的用户的回答", description = "获取我关注的用户的回答")
    @GetMapping("/followed")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    array = @ArraySchema(schema = @Schema(implementation = AnswerVo.class))
            )
    )
    public Result<PageVo<AnswerVo>> getFollowedAnswers(@Parameter(name = "page", description = "页码")
                                              @RequestParam(defaultValue = "1", required = false) int page,
                                                   @Parameter(name = "size", description = "每页数量")
                                              @RequestParam(defaultValue = "10", required = false) int size,
                                                   HttpServletRequest request) {
        return Result.success(answerService.getFollowedAnswers(page, size, request));
    }

    @Operation(summary = "创建回答", description = "根据问题ID创建回答")
    @PostMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> createAnswer(@Parameter(name = "id", description = "问题ID", required = true, in = ParameterIn.PATH) @PathVariable Long id,
                                     @Valid @RequestBody AnswerDTO answer, HttpServletRequest request) {
        answerService.createAnswer(id, answer, request);
        return Result.success();
    }

    @Operation(summary = "更新回答", description = "根据回答ID更新回答")
    @PutMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> updateAnswer(@Parameter(name = "id", description = "回答ID", required = true, in = ParameterIn.PATH) @PathVariable Long id, @Valid @RequestBody AnswerDTO answer, HttpServletRequest request) {
        answerService.updateAnswer(id, answer, request);
        return Result.success();
    }

    @Operation(summary = "删除回答", description = "根据回答ID删除回答")
    @DeleteMapping("/{id}")
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    public Result<Void> deleteAnswer(@Parameter(name = "id", description = "回答ID", required = true, in = ParameterIn.PATH) @PathVariable Long id, HttpServletRequest request) {
        answerService.deleteAnswer(id, request);
        return Result.success();
    }
}